package com.controller;

import com.entity.*;
import com.service.AdminBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("admin")
public class AdminBookController {

    @Autowired
    AdminBookService adminBookService;

    //得到所有图书类型
    @RequestMapping("/tt")
    public String tt(){
        return "22";
    }

    //得到所有图书类型
    @RequestMapping("/getBookType")
    public Result getBookType(){
        try {
            List<BookType> bookTypeList= adminBookService.getBookType();
            return new Result(true,"查询图书类型成功",bookTypeList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询图书类型失败");
        }
    }

    //图书信息查询
    @RequestMapping("/bookDetail")
    public PageResult bookDetail(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request){
        PageResult pageResult= adminBookService.getBookDetail(queryPageBean,request);
        return pageResult;
    }

    //根据bNo查询图书信息
    @RequestMapping("/getBookById")
    public Result getBookById(Integer bNo, HttpServletRequest request){
        try{
            Books book= adminBookService.getBookById(bNo,request);
            return new Result(true,"查询成功",book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }


    }

    //修改图书信息
    @RequestMapping("/updateBook")
    public Result updateBook(@RequestBody Books book){
        System.out.println(book);
        try{
            adminBookService.updateBook(book);
            return new Result(true,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    //查看借者信息
    @RequestMapping("/getBorrowers")
    public Result getBorrowers( Integer bNo){
        try{
            List<BorrowBooks> borrowBooksList= adminBookService.getBorrowers(bNo);
            return new Result(true,"查询借者信息成功",borrowBooksList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询借者信息失败");
        }
    }

    //删除图书
    @RequestMapping("/deleteBook")
    public Result deleteBook( Integer bNo){
        try{
            adminBookService.deleteBook(bNo);
            return new Result(true,"删除图书成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除图书失败");
        }
    }

    //添加图书
    @RequestMapping("/addBook")
    public Result addBook(@RequestBody Books book){
//        System.out.println(book);
        Result result= adminBookService.addBook(book);
        return result;
    }
}
